package com.youlemall.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youlemall.common.pojo.PageResult;
import com.youlemall.mapper.BrandMapper;
import com.youlemall.pojo.Brand;
import com.youlemall.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;


import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 品牌服务接口实现类
 * 2 * @Author: kevin
 * 3 * @Date: 2019/7/12 11:49
 * 4
 */

@Service
@Transactional
public class BrandServiceImpl implements BrandService {

	@Autowired
	private BrandMapper brandMapper;

	@Override
	public void save(Brand brand) {
		brandMapper.insertSelective(brand);
	}

	@Override
	public void update(Brand brand) {
		brandMapper.updateByPrimaryKeySelective(brand);
	}

	@Override
	public void delete(Serializable id) {

	}

	/**
	 *品牌删除业务
	 * @param ids 需要删除品牌的id
	 */
	@Override
	public void deleteAll(Serializable[] ids) {
		Example example = new Example(Brand.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("id",Arrays.asList(ids));
		brandMapper.deleteByExample(example);
	}

	@Override
	public Brand findOne(Serializable id) {
		return null;
	}

	/**
	 * 品牌类下拉选择查询品牌
	 * @return
	 */
	@Override
	public List<Map<String,Object>> findAll() {
		try {
			return brandMapper.findAllByIdAndName();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**`
	 * 品牌类业务分页查询
	 * @param brand 品牌类
	 * @param page 当前页
	 * @param rows 页大小
	 * @return 分页工具类
	 */
	@Override
	public PageResult findByPage(Brand brand, int page, int rows) {
		try {
			PageInfo<Brand> pageInfo = PageHelper.startPage(page, rows).doSelectPageInfo(new ISelect() {
				@Override
				public void doSelect() {
					brandMapper.findAll(brand);
				}
			});
			return new PageResult(pageInfo.getPages(),pageInfo.getList());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
